package com.epam.webproject.restaurant.jaxb.xml;

import com.epam.webproject.restaurant.jaxb.application.Application;
import com.epam.webproject.restaurant.jaxb.entities.Bill;
import com.epam.webproject.restaurant.jaxb.entities.Bills;
import com.epam.webproject.restaurant.jaxb.utils.ComparatorBillsByAmount;
import org.apache.log4j.Logger;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Yelena_M
 * Date: 12.09.13
 * Time: 15:09
 * To change this template use File | Settings | File Templates.
 */
public class GeneratorObject {

    private final static Logger LOGGER = Logger.getLogger(Application.class);

    public static void generateBill(String file) {
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Bills.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            Bills bills = (Bills) unmarshaller.unmarshal(new File(file));
            List<Bill> listBills = bills.getRestaurantBill();
            for (Bill bill : listBills) {
                LOGGER.info("Bill: " + bill.toString());
            }

            Collections.sort(listBills, new ComparatorBillsByAmount());

            LOGGER.info("After sorting by amount: ");
            for (int i = 0; i < listBills.size(); i++) {
                Bill bill = listBills.get(i);
                LOGGER.info("Bill: " + i + " " + bill.toString());
            }

        } catch (JAXBException e) {
            LOGGER.error(e);
        }
    }
}
